package servlet;

import dao.GoodsDao;
import java.sql.ResultSet;
import model.Goods;
import model.Shopping_cart;
import util.JDBCUtil;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/Goods_cartServlet.do")
public class Goods_cartServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String user_idstring=request.getParameter("user_id");
        if (user_idstring != null && !user_idstring.isEmpty()) {
            int user_id = Integer.parseInt(user_idstring);

            int cart_id = searchCart_id("SELECT cart_id FROM shopping_cart WHERE user_id = ?", user_id);

            List<Shopping_cart> searchResults=performSearch("select * from shopping_cart where cart_id = ?", cart_id);
            HttpSession session = request.getSession();
            session.setAttribute("searchResults", searchResults);
            response.sendRedirect(request.getContextPath() + "/Front/cart.jsp");
        }else {
            request.setAttribute("error", " Invalid ID format");
            request.getRequestDispatcher("home/404.html").forward(request, response);
            return;
        }


    }



    private int searchCart_id(String sql, Object parameter) {

        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = null;
        int cart_id = 0;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, parameter);
            if (resultSet != null && resultSet.next()) {
                cart_id = resultSet.getInt("cart_id");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cart_id;

    }
    private List<Shopping_cart> performSearch(String sql, int cart_id) {
        List<Shopping_cart> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, cart_id);

            while (resultSet != null && resultSet.next()) {
                int goods_id = resultSet.getInt("goods_id");
                int user_id = resultSet.getInt("user_id");

                int quantity = resultSet.getInt("quantity");

                userList.add(new Shopping_cart(goods_id, user_id, cart_id, quantity));
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return userList;
    }

}

